package com.frank.activedatabus.broadcast;

import ohos.event.commonevent.CommonEventSubscribeInfo;
import ohos.event.commonevent.CommonEventSubscriber;
import ohos.event.commonevent.MatchingSkills;

/**
 * @version 1.0.1
 * @description: 订阅事件获取工具
 * @program: activedatatest
 * @Author xiaozhijun
 * @Date 2020/12/23 10:49
 */
public class SubscriberObtain {


    /**
     * 获取订阅事件
     *
     * @param action   action
     * @param priority 优先级
     */
    public static CommonEventSubscriber getSubscriber(String action, int priority) {
        MatchingSkills matchingSkills = new MatchingSkills();
        matchingSkills.addEvent(action); // 自定义事件

        CommonEventSubscribeInfo subscribeInfo = new CommonEventSubscribeInfo(matchingSkills);
        subscribeInfo.setPriority(priority); // 设置优先级，优先级取值范围[-1000，1000]，值默认为0。
        BroadCastEventSubscriber subscriber = new BroadCastEventSubscriber(subscribeInfo);
        System.out.println("是否是黏性事件：" + subscriber.isStickyCommonEvent());
        return subscriber;
    }
}
